package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.NursingPlanMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.nursing.NursingLevelParam;
import com.itheima.pojo.nursing.NursingPlan;
import com.itheima.pojo.nursing.NursingPlanParam;
import com.itheima.service.NursingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    //@Override
    //public PageResult<NursingPlan> page(NursingLevelParam param){
    //    // 1. 创建 MyBatis-Plus 分页对象
    //    com.baomidou.mybatisplus.extension.plugins.pagination.Page<NursingPlan> page
    //            = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(param.getPage(), param.getPageSize());
    //
    //    // 2. 执行分页查询（需确保 Mapper 中方法支持分页）
    //    nursingPlanMapper.selectPage(page, null);
    //
    //    // 3. 返回分页结果
    //    return PageResult.<NursingPlan>builder()
    //            .total(page.getTotal())
    //            .rows(page.getRecords())
    //            .build();
    //}

    //@Override
    //public PageResult<NursingPlan> page(NursingPlanParam nursingPlanParam) {
    //    // 1. 创建 MyBatis-Plus 分页对象
    //    com.baomidou.mybatisplus.extension.plugins.pagination.Page<NursingPlan> page
    //            = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(nursingPlanParam.getPage(), nursingPlanParam.getPageSize());
    //
    //    // 2. 执行分页查询（需确保 Mapper 中方法支持分页）
    //    nursingPlanMapper.selectPage(page, null);
    //
    //    // 3. 返回分页结果
    //    return PageResult.<NursingPlan>builder()
    //            .total(page.getTotal())
    //            .rows(page.getRecords())
    //            .build();
    //}
    @Override
    public PageResult<NursingPlan> page(NursingPlanParam nursingPlanParam) {
        // 1. 创建 MyBatis-Plus 分页对象
        Page<NursingPlan> page = new Page<>(nursingPlanParam.getPage(), nursingPlanParam.getPageSize());

        // 2. 创建查询条件
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        if (nursingPlanParam.getPlanName() != null && !nursingPlanParam.getPlanName().isEmpty()) {
            queryWrapper.like(NursingPlan::getPlanName, nursingPlanParam.getPlanName());
        }

        // 3. 执行分页查询（使用MyBatis-Plus的分页查询方法）
        Page<NursingPlan> resultPage = nursingPlanMapper.selectPage(page, queryWrapper);

        // 4. 返回分页结果
        return PageResult.<NursingPlan>builder()
                .total(resultPage.getTotal())
                .rows(resultPage.getRecords())
                .build();
    }
}
